/*
 * Wevo2 - Distributed Evolutionary Computation Library.
 * Copyright (C) 2009 Marcin Brodziak
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 *    Boston, MA  02110-1301  USA
 */
package engine.operators.binary;

import its.util.RandomFactory;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import engine.Operator;
import engine.Population;
import engine.individuals.ProgramedTrafficNetIndividual;

/**
 * Performs mutation of the individual by randomly negating one 
 * bit with some probability.
 * @author Marcin Brodziak (marcin@nierobcietegowdomu.pl)
 */
public class TrafficMutation 
    implements Operator<ProgramedTrafficNetIndividual> {
 
  /** For each bit, probability of negating it. */
  private final double mutationProbability;
 
  /**
   * Returns a mutator, which modifies each bit of the {@link ProgramedTrafficNetIndividual}
   * negated with some probability.
   * @param mutationProbability Probability of the mutation of each bit 
   *     of the individual.
   */
  public TrafficMutation(double mutationProbability) {
    this.mutationProbability = mutationProbability;
  }


  /** {@inheritDoc} */
  public Population<ProgramedTrafficNetIndividual> 
      apply(Population<ProgramedTrafficNetIndividual> population) {
    Iterator<ProgramedTrafficNetIndividual> iterator = population.getIndividuals().iterator();
    List<ProgramedTrafficNetIndividual> output = new LinkedList<ProgramedTrafficNetIndividual>();
 
    while (iterator.hasNext()) {
      ProgramedTrafficNetIndividual ind = iterator.next();
      
      if (RandomFactory.getNextDouble() < mutationProbability) {
      	ind.mutate();
      } 
      
      ind.heal();
      
      output.add(ind);
    }
    return population;
  }

}
